package org.example.schoolbus.controller;

import org.example.schoolbus.entity.ResultBody;
import org.example.schoolbus.service.QueryService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Query")
@CrossOrigin(origins = "*")
public class QueryController {
    @Autowired
    private QueryService queryService;

    @RequestMapping("/QueryUser")
    public ResultBody QueryUser(@RequestBody Map<String, Object> map) {
        if (map.get("station_name") == null && map.get("route_name") == null) {
            //没有写入参数时
            return new ResultBody(404, "没有搜索内容", false);
        }
        if (map.get("station_name") != null) {
            //传入站名
            String station_name = map.get("station_name").toString();
            List<Map<String, Object>> maps = queryService.sQueryUser(station_name);
            if (maps.size() > 0) {
                //表中查询到了相关信息
                return new ResultBody(200, "搜索成功", maps, maps.size());
            }
            else
                //表中未查寻到相关信息
                return new ResultBody(400, "查询失败", false);
        } else if (map.get("route_name") != null) {
            //传入线路名
            String route_name = map.get("route_name").toString();
            List<Map<String, Object>> mapr = queryService.rQueryUser(route_name);
            if (mapr.size()>0) {
                //表中查寻到相关信息
                return new ResultBody(200, "搜索成功", mapr, mapr.size());
            }
            //表中未查询到相关信息
            else
                return new ResultBody(400, "查询失败", false);
        } else
            return new ResultBody(400, "查询失败", false);
    }
}
